using System;
using System.Reflection;
using UnityEngine;

namespace SCore.Editor.WindowDraw
{
    /// <summary>
    /// 代表一个GUI裁剪的反射类型
    /// </summary>
    public static class EditorClip
    {
        static Lazy<GUIClip_Push> PushMethod = new(() =>
        {
            var a = Assembly.Load("UnityEngine");
            var t = a.GetType("UnityEngine.GUIClip");
            var m = t.GetMethod(nameof(Push), BindingFlags.Static | BindingFlags.NonPublic);
            var d = m.CreateDelegate(typeof(GUIClip_Push), null);
            return (GUIClip_Push)d;
        });

        static Lazy<GUIClip_Pop> PopMethod = new(() =>
        {
            var a = Assembly.Load("UnityEngine");
            var t = a.GetType("UnityEngine.GUIClip");
            var m = t.GetMethod(nameof(Pop), BindingFlags.Static | BindingFlags.NonPublic);
            var d = m.CreateDelegate(typeof(GUIClip_Pop), null);
            return (GUIClip_Pop)d;
        });

        public static void Push(Rect screenRect)
        {
            Push(screenRect, Vector2.zero, Vector2.zero, false);
        }

        public static void Push(Rect screenRect, Vector2 scrollOffset, Vector2 renderOffset, bool resetOffset)
        {
            PushMethod.Value.Invoke(screenRect, scrollOffset, renderOffset, resetOffset);
        }

        public static void Pop()
        {
            PopMethod.Value.Invoke();
        }

        public delegate void GUIClip_Push(Rect screenRect, Vector2 scrollOffset, Vector2 renderOffset,
            bool resetOffset);

        public delegate void GUIClip_Pop();
    }
}